//
//  OPReadViewController.m
//  MyOne3
//
//  Created by meilbn on 2/20/16.
//  Copyright © 2016 meilbn. All rights reserved.
//

#import "OPReadViewController.h"

#import "OPReadTopImage.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "OPReadToViewCell.h"

#import "OPReadImageDetail.h"


@interface OPReadViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
/**collectionView*/
@property (nonatomic, strong) UICollectionView *collectionView;
//模型数组
@property (strong, nonatomic) NSMutableArray *readArrM;

@end

@implementation OPReadViewController

#pragma mark - lazy collectionView
- (UICollectionView *)collectionView
{
    if (!_collectionView) {

        //使用流水布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

        layout.itemSize = CGSizeMake(SCREEN_WIDTH, 200);

        //设置水平滚动
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        //设置间距
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;      //上下之间的间距

#warning 设置frame 总是说有问题
        //创建collectionView
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 200) collectionViewLayout:layout];


        //BG
        collectionView.backgroundColor = [UIColor redColor];

        //去掉指示条
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;

        //开启分页
        collectionView.pagingEnabled = YES;

        //设置数据源和代理
        collectionView.delegate = self;
        collectionView.dataSource = self;

        //添加都父控件
        [self.view addSubview:collectionView];

        //添加约束
//        [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(self.view);
//        }];

        _collectionView = collectionView;

    }
    return _collectionView;
}

//模型数组懒加载
- (NSMutableArray *)readArrM
{
    if (!_readArrM) {

        _readArrM = [NSMutableArray array];

    }
    return _readArrM;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



static NSString * const topImageID = @"OPReadToViewCell";

#pragma mark - View Lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = OPGlobalColor;

    [self.collectionView registerClass:[OPReadToViewCell class] forCellWithReuseIdentifier:topImageID];

    self.title = @"阅读";
    [self loadCacheTopImage];

}
//查看沙盒是否有数据  有的话就直接赋值
- (void)loadCacheTopImage{
    // 解档数据
    id cacheTopImage = [NSKeyedUnarchiver unarchiveObjectWithFile:MLBCacheReadCarouselFileName];
    // 如果有数据就赋值给
    if (cacheTopImage) {
        self.readArrM = cacheTopImage;
        // 刷新数据
        [self.collectionView reloadData];

        return;
    }

    //如果没有就联网获取获取数据
    //获取数据
    [self getData];
}


- (void)getData
{
    [MLBHTTPRequester requestReadCarouselWithSuccess:^(id responseObject) {

        //        NSLog(@"%@", responseObject);
        //        [responseObject writeToFile:@"/Users/mac/Desktop/一个人/阅读头部轮播列表.plist" atomically:YES];

        self.readArrM = [OPReadTopImage mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];


        [self.collectionView reloadData];

    } fail:^(NSError *error) {

        NSLog(@"%@", error);
        
    }];
}



#pragma mark - collectionView dataSorce
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.readArrM.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OPReadToViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:topImageID forIndexPath:indexPath];

    cell.topImageItem = self.readArrM[indexPath.row];

    return cell;
}

#pragma mark - collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    OPReadTopImage *item = self.readArrM[indexPath.row];

    OPReadImageDetail *detailVC = [[OPReadImageDetail alloc] init];
    detailVC.Id = item.id;

    [self presentViewController:detailVC animated:YES completion:nil];


}



@end
